<?php
	class Admins extends MY_Controller {
		function Admins() {
			parent::__construct();
			@session_start();
			$this->mod_ad->is_logged_in();	
			$_SESSION['redirect'] = $this->uri->uri_string();
			$this->get_link();
			//kiem tra quyen truy cap 
			$this->check_permission();	
		}
		
		function index(){	
			$data['vl_sort'] = "DESC";	
			$data['title'] = "Tài khoản admin";
			$data['table'] = 'admins';
			$data['controller'] = 'admins';
			$this->db->order_by('parent_id','asc');
			$data['content_admin'] = 'back_end/admins/index';
			$data['sort'] = 'ASC';
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
					
			$per_page = 20;
			$uri = 4;
			$data['admins_value'] = $this->mod_lib->load_all('admins','',array(),$per_page,$this->uri->segment($uri),array("id"=>"desc"));																													
			$config['base_url']     =  site_url('back_end/admins/index/');
			$config['total_rows']   =  $this->db->count_all('admins');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			
			$this->pagination->initialize($config);
			$this->load->view('back_end/template_backend',$data);		
		}
		
		function sort($field,$sort = '') {
				$per_page = 10;
				$uri = 4;
				$data['title'] = "Sắp xếp thành viên";
				$data['table'] = 'admins';
				$data['controller'] = 'admins';
				$data['content_admin'] = 'back_end/admins/index';
				$this->db->order_by('parent_id','asc');
		
				//lay ra cac danh muc cap 1
				$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
				//lay ra cac danh muc cap 2
				$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
				//lay ra category hien tai
				$uri_value = $this->uri->segment(2);
				$this->db->select('root_path,id');
				$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
				
				$uri_value_4 =  $this->uri->segment(4);
				$uri_value_5 =  $this->uri->segment(5);
				
				$_SESSION['order_sort'] = array(
					$uri_value_4 => $uri_value_5 
				);
				
				$_SESSION['sort'] = $sort;
				$_SESSION['field'] = $field;
				
				if ($sort == 'DESC') {
					$data['sort'] =  'ASC';
				} else if($sort == 'ASC') {
					$data['sort'] =  'DESC';
				}
				
				$data_search = $this->mod_lib->search('admins','',array(),array(),array("id"=>"desc"));
				$data['admins_value'] = $this->mod_lib->load_search('admins','',array(),array(),$per_page,$this->uri->segment($uri),array($_SESSION['field']=>$_SESSION['sort']));		
				$config['base_url']     =  site_url('back_end/admins/index');
	
				$config['total_rows']   =  count($data_search);
				$config['per_page']     =  $per_page;
				$config['uri_segment']  =  $uri; 
				$this->pagination->initialize($config);		
				$this->load->view('back_end/template_backend',$data);
			}
			
		/**
		 * @desc: Phan quuyen su dung cho nguoi dung he thong
		 */
		function permission($id){
			$data['title'] 			= " Phân quyền cho thành viên quản trị";
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['content_admin']  = 'back_end/admins/index';	
			$this->db->where('id',$id);
			$data['admin']          = $this->db->get('admins')->row_array();
			$data['admin_id']		= $id;
			$data['admin_user']		= $data['admin']['username'];
			$data['content_admin']	= 'back_end/admins/permission';
			
			$this->db->select('permiss_admins.*,admin_menu.name,admin_menu.controller,admin_menu.id AS id_menu');
			$this->db->from('permiss_admins');
			$this->db->join('admin_menu','permiss_admins.admin_menu_id = admin_menu.id');
			$this->db->where('permiss_admins.admins_id',$id);
			$data['menu']			= $this->db->get()->result_array();
		
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			//xoa nhung admin nao co controller = #
			$this->db->select('id'); 
			$this->db->where('controller','#');
			$controller_delete = $this->db->get('admin_menu')->result_array();
			
			foreach ($controller_delete  as $controller_delete_arr){
				$this->db->delete('permiss_admins', array('admin_menu_id' => $controller_delete_arr['id'])); 
			}
			
			//lay ra tat ca cac controller cua admin_menu
			$this->db->select('id'); 
			$this->db->where('controller <>','#');
			$controller_admin_menu = $this->db->get('admin_menu')->result_array();
			foreach ($controller_admin_menu  as $controller_admin_menu_arr) {
				$controller_admin_menu_view[] = $controller_admin_menu_arr['id'];
			}
			
			//lay ra tat ca cac controller cua permiss_admins
			$this->db->select('permiss_admins.admin_menu_id');
			$this->db->from('permiss_admins');
			$this->db->join('admin_menu','permiss_admins.admin_menu_id = admin_menu.id');
			$this->db->where('permiss_admins.admins_id',$id);
			$controller_admin = $this->db->get()->result_array();
			foreach ($controller_admin as $controller_admin_arr) {
				$controller_admin_view[] = $controller_admin_arr['admin_menu_id'];
			}
			
			$controller_diff = array_diff($controller_admin_menu_view, $controller_admin_view);
			
			if (count($controller_diff ) > 1) {
				foreach ($controller_diff  as $controller_diff_arr) {
					$data_value = array (
						'admin_menu_id'   => $controller_diff_arr,
						'admins_id'       => $id				
					);	
					$this->mod_lib->insert('permiss_admins',$data_value);
				} 	
			}
			
			if($this->input->post('add')){
				foreach ($data['menu'] as $menu){
					$admins_id = $menu['admins_id'];
					$admin_menu_id 	 = $menu['admin_menu_id'];
					$Index 	= 0;
					$Add 	= 0;
					$Edit 	= 0;
					$Delete = 0;
					
					$Input_Index   = $this->input->post("Index_".$admins_id.'_'.$admin_menu_id);
					$Input_Add     = $this->input->post("Add_".$admins_id.'_'.$admin_menu_id);
					$Input_Edit    = $this->input->post("Edit_".$admins_id.'_'.$admin_menu_id);
					$Input_Delete  = $this->input->post("Delete_".$admins_id.'_'.$admin_menu_id);
					
					if($Input_Index == 1){
						$Index = 1; 
					}
					if($Input_Add  == 1) {
						$Add   = 1;		
					}
					if($Input_Edit == 1){
						$Edit 	= 1;		
					}
					if($Input_Delete == 1){
						$Delete 	= 1;	
					}
					
					$where = array(
			  			'id' => $menu['id']
			  		);
			  		$order = array(
			  			'id' => 'DESC'
			  		);		
					$data_value = array(
						'index'  => $Index,
						'add' 	 => $Add,
						'edit'   => $Edit,
						'delete' => $Delete,
					);
	
					if($this->mod_lib->update('permiss_admins',$where,$order,$data_value)){
						$this->session->set_flashdata('msview', 'Thay đổi quyền thành công !');	
					} else {
						$this->session->set_flashdata('msview', 'Thay đổi quyền thất bại !');
					}
				}			
				
				redirect('back_end/admins/permission/'.$id);
			}

			$this->load->view('back_end/template_backend',$data);
		}
		
		function add(){	
			$data['title'] = "Thêm thành viên quản trị";
			$data['table'] = 'admins';
			$data['controller'] = 'admins';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['content_admin'] = 'back_end/admins/add';
			$add = $this->input->post('addform');
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			if(!empty($add)){ 
				$data_value = array (
						'username'       => trim($this->input->post('username')),
						'fullname'       => trim($this->input->post('fullname')),
						'password'       => md5(trim($this->input->post('password'))),  
						'status'         => trim($this->input->post('status')),
						'email'          => trim($this->input->post('email')) 				
					);

				if($this->mod_lib->insert('admins',$data_value)) {
					$idAdmin = $this->db->insert_id();
					$this->db->select('id');
					$this->db->where('level >', 1);
					$admin_menu = $this->db->get('admin_menu')->result_array();
					
					foreach ($admin_menu  as $menu) {
						$data_menu = array (
							'admin_menu_id'       => $menu['id'],
							'admins_id'           => $idAdmin,
							'delete'              => 0,
							'add' 		          => 0,
							'edit'                => 0,  			
						);		
									
						$this->mod_lib->insert('permiss_admins',$data_menu);
					}
					
					$this->session->set_flashdata('msview', 'Thêm thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Thêm thất bại !');
				}
				
				redirect('back_end/admins');	
			} else {
				$this->load->view('back_end/template_backend',$data);
			}		
		}
		
		function edit($id = null){
			$data['title']			= "Sửa thông tin quản trị viên";
			$data['content_admin'] = 'back_end/admins/edit';
			$data['admins_value'] = $this->db->get_where('admins',array( 'id' => $id ))->result_array();
			$edit = $this->input->post('editForm');
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			if(!empty($edit)) {
				$where = array(
			  			'id' => $id 
			  		);
			  	$order = array(
			  			'id' => 'DESC'
			  		);		
				$data_value = array(
						'username' => trim($this->input->post('username')),
						'fullname' => trim($this->input->post('fullname')),
						'email' => trim($this->input->post('email')),
						'status' => trim($this->input->post('status')),
						'password' => trim(md5($this->input->post('password')))
					);
					
				$this->mod_lib->update('admins',$where,$order,$data_value);
				
				redirect('back_end/admins');
			} else {
				$this->load->view('back_end/template_backend',$data);	
			}
		}
		 
		function delete(){
			$value_id_arr    = $this->input->post('value_id');
			
			if(!empty($value_id_arr)){
				$value_id_string = implode(',',$value_id_arr);
				foreach ($value_id_arr as $key => $value) {
					$this->db->where('admins_id',$value);
					$this->db->delete('permiss_admins');
				}
				
				if($this->mod_lib->deleteRecord('admins',$value_id_string)){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}
			
			redirect('back_end/admins');
		}
		
		function deleteId($id) {
			if(!empty($id)){
				if($this->mod_lib->deleteRecord('admins',$id)){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}		
			redirect('back_end/admins');
		}
	}
?>